﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using P2P.Main.Model;
using P2P.Main.Session;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using P2P.Network.ExtensionMethods;

namespace P2P.Main.Handler
{
    /// <summary>
    /// 广播搜索处理Handler
    /// </summary>
    public class BroadcastSearchHandler
    {
        private ApiProtocol apiProtocol;

        public BroadcastSearchHandler(ApiProtocol apiProtocol)
        {
            this.apiProtocol = apiProtocol;
        }

        /// <summary>
        /// 文件搜索广播
        /// </summary>
        public void FileSearch() 
        {
            string accountId = (string)apiProtocol.Params["AccountId"];

            ActiveNode? activeNode = ActiveNodeSessionManager.Instance.Find(accountId);

            if (activeNode == null)
            {
                return;
            }

            string searchTerm = (string)apiProtocol.Params["SearchTerm"];

            Dictionary<string,ActiveNode> activeNodeDict = new Dictionary<string,ActiveNode>(ActiveNodeSessionManager.Instance.ActiveNodeDict);

            activeNodeDict.Remove(accountId);

            JObject data = new JObject();
            data.Add("Name", "BroadcastSearch");
            data.Add("Method", "BroadcastSearchFile");
            data.Add("Version", "1.0");

            JObject param = new JObject();
            param.Add("SearchTerm", searchTerm);
            param.Add("SearchAccountId", accountId); 
            data.Add("Params", param);

            string searchJson = JsonConvert.SerializeObject(R<JObject>.Ok(data));

            activeNodeDict.Values.ToList().ForEach(node => 
            {
                apiProtocol.Socket.SendTo(searchJson,node.IP,node.UdpPublicPort);
            });
        }

        /// <summary>
        /// 文件搜索结果推送搜索者
        /// </summary>
        public void FileSearchResultList() 
        {
            string accountId = (string)apiProtocol.Params["AccountId"];

            ActiveNode? activeNode = ActiveNodeSessionManager.Instance.Find(accountId);

            if (activeNode == null)
            {
                return;
            }

            string searcherAccountId = (string)apiProtocol.Params["SearcherAccountId"];
            ActiveNode? searcherActieNode = ActiveNodeSessionManager.Instance.Find(searcherAccountId);

            if (searcherActieNode == null)
            {
                return;
            }

            string fileList = (string)apiProtocol.Params["FileList"];

            JObject data = new JObject();
            data.Add("Name", "BroadcastSearch");
            data.Add("Method", "FileList");
            data.Add("Version", "1.0");

            JObject param = new JObject();
            param.Add("FileList", fileList);
            data.Add("Params", param);

            string searchJson = JsonConvert.SerializeObject(R<JObject>.Ok(data));

            apiProtocol.Socket.SendTo(searchJson, searcherActieNode.IP,searcherActieNode.UdpPublicPort); 
        }
    }
}
